package com.edfapp.costom;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author hong
 * @description 描述
 * @time 2025/9/26 12:07
 **/
public class CSVDataExtractor {
    public static List<List<String>> extractDataFromCsv(String filePath) {
        List<List<String>> dataList = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine())!= null) {
                // 假设字段值之间用空格分隔，根据实际情况调整分隔符
                String[] parts = line.split(",");
                List<String> rowData = new ArrayList<>();
                rowData.add(parts[4]); rowData.add(parts[6]);
//                for (String part : parts) {
//                    rowData.add(part);
//                }
                dataList.add(rowData);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataList;
    }

    public static List<CSVBrainWave> extractDataFromCsvData(String filePath) {
        List<CSVBrainWave> dataList = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine())!= null) {
                // 假设字段值之间用空格分隔，根据实际情况调整分隔符
                String[] parts = line.split(",");
                CSVBrainWave wave = new CSVBrainWave(parts[4]);
                String[] s = parts[6].split(" ");
                List<Double> doubles = new ArrayList<>();
                for (int i = 1; i < s.length; i++) {
                    doubles.add(Double.parseDouble(s[i]));
                }
                wave.setData(doubles);
                dataList.add(wave);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dataList;
    }

    public static void main(String[] args) {
        String filePath = "C:\\Users\\xinho\\Desktop\\edf文件\\2024010516.csv";
        List<CSVBrainWave> result = CSVDataExtractor.extractDataFromCsvData(filePath);
        for (CSVBrainWave row : result) {
            System.out.println(row);
        }
        System.out.println("-----------------------------------------");
        if (!result.isEmpty()){
            List<CSVBrainWave> rList = new ArrayList<>();
            Map<String, List<CSVBrainWave>> collect = result.stream().collect(Collectors.groupingBy(CSVBrainWave::getCh));
            collect.forEach((key, value)->{
                    List<Double> doubles = new ArrayList<>();
                    for (CSVBrainWave wave: value){
                        doubles.addAll(wave.getData());
                    }
                    Double min = Collections.min(doubles);
                    Double max = Collections.max(doubles);
                CSVBrainWave t = new CSVBrainWave(key, doubles, min, max);
                rList.add(t);
            });
            for (CSVBrainWave row : rList) {
                System.out.println(row);
            }
            System.out.println("-----------------------------------------");
        }
    }
}
